package com;

import com.spaceprogram.kittycache.KittyCache;

public class SharedCache {
	static KittyCache sharedCache = new KittyCache(500);
	static KittyCache hotelCache = new KittyCache(2500);
	
	public static void ClearAllCache(){
		sharedCache = new KittyCache(500);
		hotelCache = new KittyCache(2500);
	}
	
	public static void setSharedCache(String key,Object value,int secondsToLive){
		sharedCache.put(key, value, secondsToLive);
	}
	
	public static Object getSharedCache(String key){
		return sharedCache.get(key);
	}
	
	public static void setHotelCache(long hotelId,String key,Object value,int secondsToLive){
		hotelCache.put(hotelId+"_"+key, value, secondsToLive);
	}
	
	public static Object getHotelCache(long hotelId,String key){
		return hotelCache.get(hotelId+"_"+key);
	}
	
	public static void clearHotelCache(long hotelId){
		Object[] keys = hotelCache.GetCacheKey().toArray();
		for(int i=0;i<keys.length;i++)
			if(keys[i].toString().startsWith(hotelId+"_"))
				hotelCache.remove(keys[i]);
	}
	
	public static void clearSharedCache(String key){
		Object[] keys = sharedCache.GetCacheKey().toArray();
		for(int i=0;i<keys.length;i++)
			if(keys[i].toString().equals(key))
				sharedCache.remove(keys[i]);
	}
	
	public static void clearHotelCacheStartWith(long hotelId,String startwith){
		Object[] keys = hotelCache.GetCacheKey().toArray();
		for(int i=0;i<keys.length;i++)
			if(keys[i].toString().startsWith(hotelId+"_"+startwith))
				hotelCache.remove(keys[i]);
	}
}
